#coding:utf-8
#code by foRK
#QQ:261496481

import sys
import time
import re
import hashlib

if len(sys.argv) < 2:
	print '\nUsage:'
	print '\t%s --help Display this help' % sys.argv[0]
	print '\t%s --version ' % sys.argv[0]
	print '\t%s --crack [hash] [dictionary] ' % sys.argv[0]
	sys.exit(1)
	
def fork():
	print '''
	|-------------------------------|
	|	[+] MD5 Hash Crack Offline  |
	|	[+] Code By foRK            |
	|	[+] 2013/09/14              |
	|-------------------------------|
'''

option = sys.argv[1]


if option == '--help':
	print '\nUsage:'
	print '\t%s --help Display this help' % sys.argv[0]
	print '\t%s --version' % sys.argv[0]
	print '\t%s --crack [hash] [dictionary] ' % sys.argv[0]

elif option == '--version': #版本号
	print 'Version 0.1'

elif option == '--crack':
	fork()
	try:
		def crack():
			print '[+] This opertaion will take some time, please wait ...' 
			passwd = sys.argv[2] #
			dictionary = sys.argv[3] #
			dic = {} #空字典
			shooter = 0
			try:
				f = open(dictionary, 'rb') #只读，二进制打开文件
				start = time.time()
				for line in f: #
					line = line.rstrip() #
					dic[line] = hashlib.md5(line).hexdigest() #
				for k in dic.keys(): 
					if passwd in dic[k]:
						stop = time.time()
						global spent
						spent = stop - start
						print '\n[-] Hash: %s\t\tData: %s\t\tTime: %.f seconds' % (dic[k], k, spent)
						shooter += 1
				if shooter == 0:  print "\n[*]Password not found in [%s] try the online cracker\n" % dictionary
				f.close()
			except IOError: print '\n[*] Erorr: %s doesn\'t exsit \n' % dictionary
		crack()
	except KeyboardInterrupt: print '\nTerminated by user ...'

  
else: pass 